package com.ubing.boot.notesforjava.test.juc;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName : test_unlock
 * @Description : test_unlock
 * @Author : Xxxxx
 * @Date: 2023-03-16 14:45
 */
public class test_unlock {
    public static void main(String[] args) {

        final Object o = new Object();

        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i <5 ; i++) {
            new Thread(()->{
                for (int j = 0; j < 5; j++) {
                    if(j%2==0){
                        synchronized (o){
                            strings.add(Thread.currentThread().getName());
                        }
                    }else {
                        synchronized (o){
                            strings.remove(strings.size()-1);
                        }
                    }
                }
            },"test"+i).start();
        }
        try {
            TimeUnit.SECONDS.sleep(2);
            System.out.println(strings);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
